CC=gcc
CFLAGS=-O3 -Wall -pedantic -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -std=c99
LDFLAGS=
LIBS=
MAKEDEPEND=gcc -MM
PROGRAM=WebServer

OBJS=   accesslist.o buffer.o end_of_request.o rulelist.o handle_backend_connection.o host_list.o log.o parsedate.o parse_uri.o \
	send_error_page.o write_buffer.o read_buffer.o backend_list.o configuration.o handle_http_connection.o \
	http_connection.o main.o pathlist.o stringlist.o base64.o dirlisting.o filelist.o headers.o http_server.o \
	mime.o rangelist.o url.o dnscache.o dns.o valid_domain.o bounded_string.o get_file_extension.o socket.o \
	reload_configuration.o handle_fastcgi_connection.o fastcgi.o parse_chunked_body.o

ifeq ($(shell uname), Linux)
	OBJS += epoll_event.o linux_write_file_range.o
	CFLAGS += -DHAVE_EPOLL -DHAVE_LINUX_SENDFILE -DHAVE_TCP_CORK -DHAVE_TIMEGM
	LIBS += -lresolv
endif
ifeq ($(shell uname), FreeBSD)
	OBJS += kqueue_event.o freebsd_write_file_range.o
	CFLAGS += -DHAVE_KQUEUE -DHAVE_FREEBSD_SENDFILE -DHAVE_TCP_CORK -DHAVE_TIMEGM
endif
ifeq ($(shell uname), Darwin)
	OBJS += kqueue_event.o mmap_write_file_range.o
	CFLAGS += -DHAVE_KQUEUE -DHAVE_MMAP -DHAVE_TIMEGM
	LIBS += -lresolv
endif
ifeq ($(shell uname), NetBSD)
	OBJS += kqueue_event.o mmap_write_file_range.o
	CFLAGS += -DHAVE_KQUEUE -DHAVE_MMAP -DHAVE_TIMEGM
endif
ifeq ($(shell uname), SunOS)
	OBJS += port_event.o solaris_write_file_range.o
	CFLAGS += -DHAVE_PORT -DHAVE_SOLARIS_SENDFILEV -D__EXTENSIONS__
	LIBS += -lsocket -lnsl -lsendfile -lresolv
endif

DEPS:= ${OBJS:%.o=%.d}

all: $(PROGRAM)

${PROGRAM}: ${OBJS}
	${CC} ${LDFLAGS} ${OBJS} ${LIBS} -o $@

clean:
	rm -f ${PROGRAM} ${OBJS} ${DEPS}

${OBJS} ${DEPS} ${PROGRAM} : Makefile

.PHONY : all clean

%.d : %.c
	${MAKEDEPEND} ${CFLAGS} $< -MT ${@:%.d=%.o} > $@

%.o : %.c
	${CC} ${CFLAGS} -c -o $@ $<

-include ${DEPS}
